package com.caoji.item.web;

import com.caoji.dto.PageDTO;
import com.caoji.entity.BrandDTO;
import com.caoji.item.entity.Brand;
import com.caoji.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private  BrandService brandService;

    //根据品牌id集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findbyids(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.findbyids(ids));
    }
    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findbybanid(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.findbybanid(id));
    }
    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findbybanids(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.findbybanids(id));
    }

    //分页查询品牌
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findbypage(
            @RequestParam(value = "key",required = false) String  key ,
            @RequestParam(value = "page",defaultValue = "1") Long  page ,
            @RequestParam(value = "rows",defaultValue = "5") Long  rows
    ){
        return ResponseEntity.ok(brandService.findbypage(key,page,rows));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> addform(BrandDTO brandDTO){
        brandService.savebrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据id删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteone(@PathVariable("id")Long id){
        brandService.deletebyid(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updateing(BrandDTO brandDTO){
        brandService.updateall(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}